package com.atguigui.leetcode;

/**
 * 513.找树左下角的值
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/6/22 13:16
 */
public class P513FindBottomLeftTreeValue {
    public static void main(String[] args) {
        Solution solution = new P513FindBottomLeftTreeValue().new Solution();
        // TO TEST
    }


    class Solution {
        // 记录深度
        private int deep = 0;
        // 记录结果
        private int result;

        public int findBottomLeftValue(TreeNode root) {
            getDIGui(root, deep);
            return result;
        }

        /**
         * 递归遍历所有节点
         *
         * @param root 节点
         * @param deep 深度
         */
        private void getDIGui(TreeNode root, int deep) {
            if (root == null) {
                return;
            }
            deep++;
            if (deep > this.deep) {
                this.result = root.val;
                // 更新深度
                this.deep = deep;
            }
            getDIGui(root.left, deep);
            getDIGui(root.right, deep);
        }
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


}
